package org.agora.photogallery.client.view;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class GalleryViewWidget extends Composite {

	public static class Styles {
		    static String DEFAULT = "galleryview";
		  }

	private final SimplePanel panel = new SimplePanel();
	private final FlowPanel mainPanel = new FlowPanel();
	private final HidingAnimation hidingAnimation = new HidingAnimation();
	private final FlowPanel closeHandle = new FlowPanel();
	private  int  originalWidth;
	private  int originalHeight;
	private  int originalLeft;
	private  int originalTop;
	private final ShowingAnimation showingAnimation = new ShowingAnimation();
	public GalleryViewWidget() {
		closeHandle.setStyleName("toggle");
		closeHandle.setTitle("Galleries");
		closeHandle.add(new Label("Galleries"));
		mainPanel.add(closeHandle);
		mainPanel.add(panel);
		mainPanel.setStyleName(Styles.DEFAULT);
		
		initWidget(mainPanel);
		
		
	}
	
	public void setContent(Widget w ){
		panel.clear();
		panel.add(w);
	}
	
	public void collapse(){
		hidingAnimation.run(500);
	}
	
	public void show(){
		showingAnimation .run(500);
	}
	
	private class HidingAnimation extends Animation{
		
		
		@Override
		protected void onStart() {
			super.onStart();
		}

		@Override
		protected void onUpdate(double progress) {
			double remain = 1.0-progress;
			int newWidth = (int) (mainPanel.getOffsetWidth()*remain);
			int newHeight = (int) (mainPanel.getOffsetHeight() * remain);
			int newLeft = (int) (mainPanel.getAbsoluteLeft() * remain);
			int newTop = (int) (mainPanel.getAbsoluteTop() * remain);
			mainPanel.setHeight(newHeight+"px");
			mainPanel.setWidth(newWidth+"px");
			Style style= mainPanel.getElement().getStyle();
			style.setTop(newTop, Unit.PX);
			style.setLeft(newLeft, Unit.PX);
		}
		
	}
	
	private class ShowingAnimation extends Animation {
		
		
		@Override
		protected void onUpdate(double progress){
			int newWidth = (int) (originalWidth*progress);
			int newHeight = (int) (originalHeight * progress);
			int newLeft = (int) (originalLeft * progress);
			int newTop = (int) (originalTop * progress);
			mainPanel.setHeight(newHeight+"px");
			mainPanel.setWidth(newWidth+"px");
			Style style= mainPanel.getElement().getStyle();
			style.setTop(newTop, Unit.PX);
			style.setLeft(newLeft, Unit.PX);
		}
	}

	public void setDimention() {
			//thumbnails are 160px
			int winWidth = Window.getClientWidth();
			int numImages = winWidth/2/162;
			mainPanel.setWidth(numImages*162+"px");
			DOM.setStyleAttribute(mainPanel.getElement(), "left", (winWidth-numImages*162)/2+"px");
			originalWidth = numImages*162;
			originalHeight = mainPanel.getOffsetHeight();
			originalTop = mainPanel.getAbsoluteTop();
			originalLeft = (winWidth-numImages*162)/2;
			
		
	}
	

}
